/***2024-12-31 迷途小羔羊
* 用于编写绕路行为
*/
namespace ghost
{
    export class B_Detour extends BehaviorBase
    {
        /**恢复绕路行为的执行间隔*/protected _detourInterval:number;
		/**上一次绕路行为停顿的时间戳*/protected _detourTime:number;
        /**当前绕路方向*/protected _detourDir:number;	
		/**当前绕路被阻塞次数*/protected _detourCount:number;
		/**当前绕路是否顺时针方向-1逆时针1顺时针*/public detourClockwise:number;	
		constructor(ai:AIBase)
        {
			super(ai);
			let s = this;
			s._detourDir = NaN;
		}
        public exc(t:number):void
		{
            super.exc(t);
			let s = this;			
			let moveState:MoveState;
			let add:number;
			
            let r:GameRole;
            r = s._role;	
			t = r._scene.timeTick;
			if(r._isDead)return;
			if(s.detourClockwise != s.detourClockwise)
				s.detourClockwise = Math.random() > 0.5?1:-1;
			add = s.detourClockwise;
			
			if(t - s._detourTime < s._detourInterval)
                return;
            if(t - s._detourTime > 5000)//5秒后尝试攻击
            {
                s.behaviorOut(s, BehaviorEnum.ATTACK);
                return;
            }		
			let tarAngle: number;
			let target:GameRole;						
			target = r._targetRoles[0];
			if(!r.targetIsValid())//目标失效，重新索敌
			{
				s.behaviorOut(s, BehaviorEnum.ATTACK);
				return;
			}
			r.resetTargetDir(target);
			if(s._detourDir != s._detourDir)
				s._detourDir = r._targetDirection;
			if(s._detourDir == r._targetDirection)
			{
				tarAngle = r._targetAngle;
			}
			else
				tarAngle = map2D.DirectionType.getDirectorAngle(s._detourDir);			
			moveState = r.moveDir(tarAngle);
			if(moveState.result == MoveResult.PASS && moveState.distanceX() + moveState.distanceY() >= r.speed)
			{	
				if(s._detourDir == r._targetDirection)
				{
					s.behaviorOut(s, BehaviorEnum.ATTACK);
					return;
				}			
				if(moveState.isPosChange() || !moveState.isXYChange())
				{					
					s._detourDir = s._detourDir - add;					
					if(s._detourDir < map2D.DirectionType.UP)
						s._detourDir = map2D.DirectionType.LEFT_UP;
					else if(s._detourDir > map2D.DirectionType.LEFT_UP)
						s._detourDir = map2D.DirectionType.UP;
				}
					
			}
			else
			{				
				s._detourDir = s._detourDir + add*2;
				if(s._detourDir < map2D.DirectionType.UP)
					s._detourDir = map2D.DirectionType.LEFT_UP;
				else if(s._detourDir > map2D.DirectionType.LEFT_UP)
					s._detourDir = map2D.DirectionType.UP;		
					
				
				if(r._moveStopRole)
				{					
					if(r.targetIsValid() && r._moveStopRole._camp == r._targetRoles[0]._camp)
					{						
						//遭遇敌方，标记为攻击对象，结束绕路
						r.setAtkTarget([r._moveStopRole]);
						s.behaviorOut(s, BehaviorEnum.ATTACK);
						return;
					}
				}			
				++s._detourCount;
				if(s._detourCount > 6)//碰撞多次，没绕出来，围绕方向换一下
				{
					s.detourClockwise = NaN;
                    s.behaviorOut(s, BehaviorEnum.DETOUR);					
				}
			}			
		}
        public behaviorOut(b:BehaviorBase, nextBehavior:BehaviorEnum):void
        {
            let s = this;
            if(nextBehavior == BehaviorEnum.DETOUR)            
                s.resetDetour();
            super.behaviorOut(b, nextBehavior);
        }
        public resetDetour():void
        {
            let s= this;
            let r:GameRole;
            r = s._role;	
			s._detourDir = r._targetDirection;
			s._detourCount = 0;
			s._detourTime = r._scene.timeTick;
        }
        public resetState(): void {
            super.resetState();
            let s = this;            
            s.detourClockwise = NaN;
            s._detourInterval = 100;
			s._detourCount = 0;			
        }
    }
}